class Word < ActiveRecord::Base
  
	has_many :word_definitions, :dependent => :delete_all
  has_many :dictionaries, :through => :word_definitions

  has_and_belongs_to_many :synonyms, :class_name => 'Word',
                          :join_table => 'synonyms',
                          :association_foreign_key => 'synonym_id'
  has_and_belongs_to_many :antonyms, :class_name => 'Word',
                          :join_table => 'antonyms',
                          :association_foreign_key => 'antonym_id'
  has_and_belongs_to_many :similar_words, :class_name => 'Word',
                          :join_table => 'similar_words',
                          :association_foreign_key => 'similar_word_id'
  has_and_belongs_to_many :related_words, :class_name => 'Word',
                          :join_table => 'related_words',
                          :association_foreign_key => 'related_word_id'
  has_and_belongs_to_many :rhymes, :class_name => 'Word',
                          :join_table => 'rhymes',
                          :association_foreign_key => 'rhyme_id'

  validates_presence_of :text
  validates_uniqueness_of :text

  before_validation_on_create :downcase_text

  def downcase_text
    self.text = text.to_s.downcase
  end

  # Available options:
  # :view_all -> returns all matching words when true, returns the first 5 when false
  def self.match(pattern, strategy, options = {})
    DictServiceHelper.match(pattern, strategy, options[:view_all])
  end

  def synchronise_related_terms
    BigHugeThesaurusHelper.look_up(self)
    RhymeHelper.look_up(self)
  end

  def add_related_terms(res)
    if res[:syn]
      for synonym in res[:syn]
        Word.add_as_synonyms(self, synonym)
      end
    end

    if res[:ant]
      for antonym in res[:ant]
        Word.add_as_antonyms(self, antonym)
      end
    end

    if res[:sim]
      for similar_word in res[:sim]
        Word.add_as_similar_words(self, similar_word)
      end
    end

    if res[:rel]
      for related_word in res[:rel]
        Word.add_as_related_words(self, related_word)
      end
    end

    if res[:rhy]
      for rhyme in res[:rhy]
        Word.add_as_rhymes(self, rhyme)
      end
    end
  end

  private

  def self.add_as_synonyms(word1, word2)
    word1.synonyms << word2 unless word1.synonyms.include?(word2)
    word2.synonyms << word1 unless word2.synonyms.include?(word1)
  end

  def self.add_as_antonyms(word1, word2)
    word1.antonyms << word2 unless word1.antonyms.include?(word2)
    word2.antonyms << word1 unless word2.antonyms.include?(word1)
  end

  def self.add_as_similar_words(word1, word2)
    word1.similar_words << word2 unless word1.similar_words.include?(word2)
    word2.similar_words << word1 unless word2.similar_words.include?(word1)
  end

  def self.add_as_related_words(word1, word2)
    word1.related_words << word2 unless word1.related_words.include?(word2)
    word2.related_words << word1 unless word2.related_words.include?(word1)
  end

  def self.add_as_rhymes(word1, word2)
    word1.rhymes << word2 unless word1.rhymes.include?(word2)
    word2.rhymes << word1 unless word2.rhymes.include?(word1)
  end

end